home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue56 / System / RASPhoneBook.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2000-03-04  |  14.3 KB  |  366 lines

  1. unit RASPhoneBook;
  2.  
  3. interface
  4.  
  5. uses
  6.     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, RASBase;
  7.  
  8. type
  9.     TRasDialParams = record
  10.     dwSize: Integer;
  11.     EntryName: array [0..256] of Char;
  12.     PhoneNumber: array [0..128] of Char;
  13.     CallbackNumber: array [0..128] of Char;
  14.     UserName: array [0..256] of Char;
  15.     Password: array [0..256] of Char;
  16.     Domain: array [0..15] of Char;
  17.     end;
  18.  
  19.     TRasDialParamsNT4_2000 = record
  20.     dwSize: Integer;
  21.     EntryName: array [0..256] of Char;
  22.     PhoneNumber: array [0..128] of Char;
  23.     CallbackNumber: array [0..128] of Char;
  24.     UserName: array [0..256] of Char;
  25.     Password: array [0..256] of Char;
  26.     Domain: array [0..15] of Char;
  27.         SubEntry: Integer;
  28.         CallbackId: Integer;
  29.     end;
  30.  
  31.     TRasEntry = record
  32.     dwSize, Options: Integer;                                                     // General stuff
  33.     CountryID, CountryCode: Integer;                                              // Phone/Country info
  34.     AreaCode: array [0..10] of Char;
  35.     LocalPhoneNumber: array [0..128] of Char;
  36.     AlternateOffset: Integer;
  37.     ipaddr, ipaddrDns, ipaddrDnsAlt, ipaddrWins, ipaddrWinsAlt: Integer;          // PPP/Ip
  38.     FrameSize, NetProtocols, FramingProtocol: Integer;                            // Framing
  39.     Script: array [0..Max_Path - 1] of Char;                                      // Scripting
  40.     AutodialDll: array [0..Max_Path - 1] of Char;                                 // AutoDial
  41.     AutodialFunc: array [0..Max_Path - 1] of Char;
  42.     DeviceType: array [0..16] of Char;                                            // Device
  43.     DeviceName: array [0..128] of Char;
  44.     X25PadType: array [0..32] of Char;                                            // X.25
  45.     X25Address: array [0..200] of Char;
  46.     X25Facilities: array [0..200] of Char;
  47.     X25UserData: array [0..200] of Char;
  48.     Channels: Integer;
  49.     Reserved1, Reserved2: Integer;
  50.     end;
  51.  
  52.     TRASPhoneBookManager = class (TRASBaseComponent)
  53.     private
  54.         { Private declarations }
  55.         fItemIndex: Integer;
  56.         fEntries, fDummy2: TStrings;
  57.         fPhoneBookFileName: String;
  58.         procedure SetItemIndex (Value: Integer);
  59.         function PhoneBookNameAsPChar: PChar;
  60.         procedure SetPhoneBookFileName (const Value: String);
  61.         function GetDialParameters (Index: Integer): String;
  62.         function GetEntryProperties (Index: Integer): String;
  63.         procedure SetDialParameters (Index: Integer; const Value: String);
  64.         procedure SetEntryProperties (Index: Integer; const Value: String);
  65.         function InternalGetDialParameters (var Dest: TRasDialParamsNT4_2000; var GotPassword: Bool): Integer;
  66.         function InternalGetEntryProperties (var Dest: TRASEntry): Boolean;
  67.         function InternalSetEntryProperties (var Dest: TRASEntry; BufSize: Integer): Integer;
  68.     protected
  69.         { Protected declarations }
  70.     public
  71.         { Public declarations }
  72.         constructor Create (AOwner: TComponent); override;
  73.         destructor Destroy; override;
  74.         function Add: Boolean;
  75.         function Delete: Boolean;
  76.         function Edit: Boolean;
  77.         function Rename (const NewName: String): Boolean;
  78.         function ValidateEntryName (const EntryName: String): Boolean;
  79.         procedure Refresh;
  80.     published
  81.         { Published declarations }
  82.         property ItemIndex: Integer read fItemIndex write SetItemIndex stored False;
  83.         property PhoneBookFileName: String read fPhoneBookFileName write SetPhoneBookFileName;
  84.         property Entries: TStrings read fEntries write fDummy2 stored False;
  85.         property UserName: String index 0 read GetDialParameters write SetDialParameters stored False;
  86.         property Password: String index 1 read GetDialParameters write SetDialParameters stored False;
  87.         property PhoneNumber: String index 0 read GetEntryProperties write SetEntryProperties stored False;
  88.         property DeviceType: String index 1 read GetEntryProperties write SetEntryProperties stored False;
  89.         property DeviceName: String index 2 read GetEntryProperties write SetEntryProperties stored False;
  90.     end;
  91.  
  92. procedure Register;
  93.  
  94. implementation
  95.  
  96. // TRASPhoneBook
  97.  
  98. constructor TRASPhoneBookManager.Create (AOwner: TComponent);
  99. begin
  100.     Inherited Create (AOwner);
  101.     fEntries := TStringList.Create;
  102.     Refresh;
  103. end;
  104.  
  105. destructor TRASPhoneBookManager.Destroy;
  106. begin
  107.     fEntries.Free;
  108.     Inherited;
  109. end;
  110.  
  111. procedure TRASPhoneBookManager.SetPhoneBookFileName (const Value: String);
  112. begin
  113.     if (fPhoneBookFileName <> Value) and (FileExists (Value) or (Value = '')) then begin
  114.         fPhoneBookFileName := Value;
  115.         Refresh;
  116.     end;
  117. end;
  118.  
  119. function TRASPhoneBookManager.PhoneBookNameAsPChar: PChar;
  120. begin
  121.     if fPhoneBookFileName = '' then
  122.         Result := Nil else Result := PChar (fPhoneBookFileName);
  123. end;
  124.  
  125. procedure TRASPhoneBookManager.Refresh;
  126. type
  127.     TRasEntryName = record
  128.         dwSize: Integer;
  129.     szEntryName: array [0..257] of Char;
  130.     end;
  131.  
  132.     TRasEntryName2000 = record
  133.         dwSize: Integer;
  134.     szEntryName: array [0..257] of Char;
  135.         dwFlags: Integer;
  136.         szPhonebookPath: array [0..Max_Path] of Char;
  137.     end;
  138. var
  139.     CurEntry, Buffer: PChar;
  140.     Idx, BufSize, NumEntries, EntrySize: Integer;
  141.     RasEnumEntries: function (Reserved, Phonebook, Buffer: PChar;
  142.                               var BufSize, NumEntries: Integer): Integer; stdcall;
  143. begin
  144.     if Available then begin
  145.         // First off, refresh the entries list
  146.         fEntries.Clear;
  147.         RasEnumEntries := GetProc ('RasEnumEntriesA');
  148.         EntrySize := sizeof (TRasEntryName);
  149.         if Win2000 then EntrySize := sizeof (TRasEntryName2000);
  150.  
  151.         // Make a dummy call to get the wanted buffer size
  152.         Idx := EntrySize; BufSize := sizeof (Idx);
  153.         RasEnumEntries (Nil, PhoneBookNameAsPChar, @Idx, BufSize, NumEntries);
  154.  
  155.         // Now do it for real
  156.         Buffer := AllocMem (BufSize);
  157.         try
  158.            PInteger (Buffer)^ := EntrySize;
  159.            if CallProc (RasEnumEntries (Nil, PhoneBookNameAsPChar, Buffer, BufSize, NumEntries)) then begin
  160.                CurEntry := Buffer;
  161.                 for Idx := 0 to NumEntries - 1 do begin
  162.                     fEntries.Add (CurEntry + sizeof (Integer));
  163.                     Inc (CurEntry, EntrySize);
  164.                 end;
  165.             end;
  166.         finally
  167.             FreeMem (Buffer);
  168.             if fEntries.Count > 0 then fItemIndex := 0 else fItemIndex := -1;
  169.         end;
  170.     end;
  171. end;
  172.  
  173. procedure TRASPhoneBookManager.SetItemIndex (Value: Integer);
  174. begin
  175.     if (Value >= 0) and (Value < fEntries.Count) and (fEntries.Count > 0) then fItemIndex := Value;
  176. end;
  177.  
  178. function TRASPhoneBookManager.Add: Boolean;
  179. var
  180.     RasCreatePhoneBookEntry: function (WndParent: hWnd; Phonebook: PChar): Integer; stdcall;
  181. begin
  182.     Result := False;
  183.     if Available then begin
  184.        RasCreatePhoneBookEntry := GetProc ('RasCreatePhonebookEntryA');
  185.        if Assigned (RasCreatePhoneBookEntry) then
  186.            Result := CallProc (RasCreatePhoneBookEntry (Application.Handle, PhoneBookNameAsPChar));
  187.        if Result then Refresh;
  188.     end;
  189. end;
  190.  
  191. function TRASPhoneBookManager.Delete: Boolean;
  192. var
  193.     RasDeleteEntry: function (Phonebook, EntryName: PChar): Integer; stdcall;
  194. begin
  195.     Result := False;
  196.     if Available and (fItemIndex >= 0) then begin
  197.        RasDeleteEntry := GetProc ('RasDeleteEntryA');
  198.        if Assigned (RasDeleteEntry) then Result := CallProc (RasDeleteEntry (PhoneBookNameAsPChar, PChar (fEntries [fItemIndex])));
  199.        if Result then Refresh;
  200.     end;
  201. end;
  202.  
  203. function TRASPhoneBookManager.Edit: Boolean;
  204. var
  205.     RasEditPhonebookEntry: function (WndParent: hWnd; Phonebook, EntryName: PChar): Integer; stdcall;
  206. begin
  207.     Result := False;
  208.     if Available and (fItemIndex >= 0) then begin
  209.        RasEditPhonebookEntry := GetProc ('RasEditPhonebookEntryA');
  210.        if Assigned (RasEditPhonebookEntry) then
  211.            Result := CallProc (RasEditPhonebookEntry (Application.Handle, PhoneBookNameAsPChar, PChar (fEntries [fItemIndex])));
  212.        if Result then Refresh;
  213.     end;
  214. end;
  215.  
  216. function TRASPhoneBookManager.ValidateEntryName (const EntryName: String): Boolean;
  217. var
  218.     RasValidateEntryName: function (Phonebook, EntryName: PChar): Integer; stdcall;
  219. begin
  220.     Result := False;
  221.     if Available then begin
  222.        RasValidateEntryName := GetProc ('RasValidateEntryNameA');
  223.        if Assigned (RasValidateEntryName) then Result := CallProc (RasValidateEntryName (PhoneBookNameAsPChar, PChar (EntryName)));
  224.     end;
  225. end;
  226.  
  227. function TRASPhoneBookManager.Rename (const NewName: String): Boolean;
  228. var
  229.     RasRenameEntry: function (Phonebook, OldName, NewName: PChar): Integer; stdcall;
  230. begin
  231.     Result := False;
  232.     if Available and (fItemIndex >= 0) and ValidateEntryName (NewName) then begin
  233.        RasRenameEntry := GetProc ('RasRenameEntryA');
  234.        if Assigned (RasRenameEntry) then
  235.            Result := CallProc (RasRenameEntry (PhoneBookNameAsPChar, PChar (fEntries [fItemIndex]), PChar (NewName)));
  236.        if Result then Refresh;
  237.     end;
  238. end;
  239.  
  240. function TRASPhoneBookManager.InternalGetDialParameters (var Dest: TRasDialParamsNT4_2000; var GotPassword: Bool): Integer;
  241. var
  242.     RasGetEntryDialParams: function (Phonebook: PChar; var RasDialParams: TRasDialParamsNT4_2000; var GotPassword: Bool): Integer; stdcall;
  243. begin
  244.     Result := 0;
  245.     if Available and (fItemIndex >= 0) then begin
  246.         RasGetEntryDialParams := GetProc ('RasGetEntryDialParamsA');
  247.         if Assigned (RasGetEntryDialParams) then begin
  248.             if Win2000 or WinNT4 then Dest.dwSize := sizeof (TRasDialParamsNT4_2000)
  249.             else Dest.dwSize := sizeof (TRasDialParams);
  250.             StrPCopy (Dest.EntryName, fEntries [fItemIndex]);
  251.             if CallProc (RasGetEntryDialParams (PhoneBookNameAsPChar, Dest, GotPassword)) then Result := Dest.dwSize;
  252.         end;
  253.     end;
  254. end;
  255.  
  256. function TRASPhoneBookManager.GetDialParameters (Index: Integer): String;
  257. var
  258.     GotPassword: Bool;
  259.     Params: TRasDialParamsNT4_2000;
  260. begin
  261.     if InternalGetDialParameters (Params, GotPassword) > 0 then begin
  262.         if not GotPassword then Params.Password := '---not available---';
  263.         case Index of
  264.             0:     Result := Params.UserName;
  265.             1:     Result := Params.Password;
  266.         end;
  267.     end;
  268. end;
  269.  
  270. procedure TRASPhoneBookManager.SetDialParameters (Index: Integer; const Value: String);
  271. var
  272.     GotPassword: Bool;
  273.     Params: TRasDialParamsNT4_2000;
  274.     RasSetEntryDialParams: function (Phonebook: PChar; var RasDialParams: TRasDialParamsNT4_2000; RemovePassword: Bool): Integer; stdcall;
  275. begin
  276.     if InternalGetDialParameters (Params, GotPassword) > 0 then begin
  277.         // Can't set username to an empty string.
  278.         if (Index = 0) and (Value = '') then Exit;
  279.         if Index = 0 then StrPCopy (Params.UserName, Value) else StrPCopy (Params.PassWord, Value);
  280.         RasSetEntryDialParams := GetProc ('RasSetEntryDialParamsA');
  281.         if Assigned (RasSetEntryDialParams) then
  282.             CallProc (RasSetEntryDialParams (PhoneBookNameAsPChar, Params, (Index = 1) and (Value = '')));
  283.     end;
  284. end;
  285.  
  286. function TRASPhoneBookManager.InternalGetEntryProperties (var Dest: TRASEntry): Boolean;
  287. var
  288.     EntrySize, DevInfoSize: Integer;
  289.     Buffer: array [0..10000] of Char;
  290.     RasGetEntryProperties: function (Phonebook, EntryName: PChar; var Entry; var EntrySize: Integer;
  291.                                      DevInfo: Pointer; var DevInfoSize: Integer): Integer; stdcall;
  292. begin
  293.     Result := False;
  294.     if Available and (fItemIndex >= 0) then begin
  295.         RasGetEntryProperties := GetProc ('RasGetEntryPropertiesA');
  296.         if Assigned (RasGetEntryProperties) then begin
  297.             PInteger (@Buffer)^ := sizeof (TRASEntry);
  298.             EntrySize := sizeof (Buffer);  DevInfoSize := 0;
  299.             Result := CallProc (RasGetEntryProperties (PhoneBookNameAsPChar, PChar (fEntries [fItemIndex]), Buffer, EntrySize, Nil, DevInfoSize));
  300.             if Result then Move (Buffer, Dest, sizeof (TRASEntry));
  301.         end;
  302.     end;
  303. end;
  304.  
  305. function TRASPhoneBookManager.GetEntryProperties (Index: Integer): String;
  306. var
  307.     Props: TRASEntry;
  308. begin
  309.     if InternalGetEntryProperties (Props) then begin
  310.         case Index of
  311.             0:     Result := Props.LocalPhoneNumber;
  312.             1:     Result := Props.DeviceType;
  313.             2:     Result := Props.DeviceName;
  314.         end;
  315.     end;
  316. end;
  317.  
  318. function TRASPhoneBookManager.InternalSetEntryProperties (var Dest: TRASEntry; BufSize: Integer): Integer;
  319. var
  320.     DevInfoSize: Integer;
  321.     RasGetEntryProperties: function (Phonebook, EntryName: PChar; var Entry: TRASEntry; var EntrySize: Integer;
  322.                                      DevInfo: Pointer; var DevInfoSize: Integer): Integer; stdcall;
  323. begin
  324.     Result := 0;
  325.     if Available and (fItemIndex >= 0) then begin
  326.         RasGetEntryProperties := GetProc ('RasGetEntryPropertiesA');
  327.         if Assigned (RasGetEntryProperties) then begin
  328.             Dest.dwSize := sizeof (TRASEntry);  DevInfoSize := 0;
  329.             if CallProc (RasGetEntryProperties (PhoneBookNameAsPChar, PChar (fEntries [fItemIndex]), Dest, BufSize, Nil, DevInfoSize)) then
  330.                 Result := BufSize;
  331.         end;
  332.     end;
  333. end;
  334.  
  335. procedure TRASPhoneBookManager.SetEntryProperties (Index: Integer; const Value: String);
  336. var
  337.     Props: TRASEntry;
  338.     EntrySize, DevInfoSize: Integer;
  339.     Buffer: array [0..10000] of Char absolute Props;
  340.     RasSetEntryProperties: function (Phonebook, EntryName: PChar; var Entry: TRASEntry;
  341.                                      var EntrySize: Integer; DevInfo: Pointer; var DevInfoSize: Integer): Integer; stdcall;
  342. begin
  343.     EntrySize := InternalSetEntryProperties (Props, sizeof (Buffer));
  344.     if EntrySize > 0 then begin
  345.         case Index of
  346.             0:    StrPCopy (Props.LocalPhoneNumber, Value);
  347.             1:    StrPCopy (Props.DeviceType, Value);
  348.             2:    StrPCopy (Props.DeviceName, Value);
  349.         end;
  350.  
  351.         DevInfoSize := 0;
  352.         RasSetEntryProperties := GetProc ('RasSetEntryPropertiesA');
  353.         if Assigned (RasSetEntryProperties) then
  354.             CallProc (RasSetEntryProperties (PhoneBookNameAsPChar, PChar (fEntries [fItemIndex]), Props, EntrySize, Nil, DevInfoSize));
  355.     end;
  356. end;
  357.  
  358. procedure Register;
  359. begin
  360.     RegisterComponents ('DelphiMag', [TRASPhoneBookManager]);
  361. end;
  362.  
  363. end.
  364.  
  365.  
  366.